<?php
class Application_Model_UserDetail extends Zend_Db_Table_Abstract
{
    protected $_name = 'user_detail';
    protected $_primary = 'user_detail_id';
    
    public function getNewMember($country=0, $loadmore_user=0)
    {
        $select = $this->getAdapter()->select();
        $select->from('users');
        $select->joinLeft('user_detail', 'users.user_id = user_detail.user_id');
        $select->joinLeft('user_country', 'user_country.user_id = users.user_id');
        $select->joinLeft('country', 'user_country.country_id = country.country_id');
        if($country) $select->where('country.code=?', strtoupper($country));
        if($loadmore_user) $select->where('users.user_id >?', $loadmore_user);
        $select->order('users.user_id DESC');
        $select->limit(5);
        return $this->getAdapter()->fetchAll($select);
    }
    
    public function getGoingViral($country=0, $loadmore_user=0, $page=1)
    {
    	$limit = 5;
        $select = $this->getAdapter()->select();
//         $select->from('users');
//         $select->joinLeft('user_detail', 'users.user_id = user_detail.user_id');
//         $select->joinLeft('user_country', 'user_country.user_id = users.user_id');
//         $select->joinLeft('country', 'user_country.country_id = country.country_id');

        $select->from('users')
        		->joinLeft('user_detail', 'users.user_id = user_detail.user_id')
                ->joinLeft('user_country', 'user_country.user_id = users.user_id')
                ->joinLeft('country', 'user_country.country_id = country.country_id');
        
        if($country) $select->where('country.code=?', strtoupper($country));
        if($loadmore_user) $select->where('users.user_id >?', $loadmore_user);
        $select->order('users.user_id DESC');
        $select->limitPage($page, $limit);
//echo $select->__toString();exit;
        return $this->getAdapter()->fetchAll($select);
    }
    
    public function getTopMember($country=0, $loadmore_user=0, $page=1)
    {
    	$limit = 5;
        $select = $this->getAdapter()->select();
        $select->from('users')
        		->joinLeft('user_detail', 'users.user_id = user_detail.user_id')
        		->joinLeft('user_country', 'user_country.user_id = users.user_id')
        		->joinLeft('country', 'user_country.country_id = country.country_id');
        
        if($country) $select->where('country.code=?', strtoupper($country));
        if($loadmore_user) $select->where('users.user_id >?', $loadmore_user);
        
        $select->order('users.user_id DESC');
        $select->limitPage($page, $limit);
//echo $select->__toString();exit;
        return $this->getAdapter()->fetchAll($select);
    }
    
    public function filerUser($data)
    {
        $select = $this->getAdapter()->select();
        $select->from('users');
        $select->joinLeft('user_detail', 'users.user_id = user_detail.user_id');
        $select->joinLeft('user_country', 'user_country.user_id = users.user_id');
        $select->joinLeft('country', 'user_country.country_id = country.country_id');
        
        if(trim($data['name']) != '') $select->where('user_detail.first_name like ?', "%" . $data['name'] . "%");
        if($data['gender'] != -1) $select->where('user_detail.gender =?', $data['gender']);
        if($data['country'] != -1) $select->where('user_country.country_id=?', $data['country']);
        $select->order('users.user_id DESC');
        $select->limit(5);
//echo $select->__toString();exit;
        return $this->getAdapter()->fetchAll($select);
    }
}